package cn.tedu.boot04.controller;

import cn.tedu.boot04.entity.User;
import cn.tedu.boot04.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public String reg(User user) {
        System.out.println("user = " + user);
        if (user.getUsername() == null || user.getUsername().isEmpty() ||
                user.getPassword() == null || user.getPassword().isEmpty()) {
            return "注册信息输入错误";
        }
        if (mapper.selectByUsername(user.getUsername()) != null) {
            return "用户名已存在";
        }
        mapper.insert(user);
        return "注册成功";
    }

    @RequestMapping("login")
    public String login(User user) {
        if (user.getUsername() == null || user.getUsername().isEmpty() ||
                user.getPassword() == null || user.getPassword().isEmpty()) {
            return "登录信息输入错误";
        }
        if (mapper.selectByUsername(user.getUsername()) != null) {
            if (user.getPassword().equals(mapper.selectByUsername(user.getUsername()).getPassword())) {
                return "登录成功";
            }
            return "密码错误";
        }
        return "用户名不存在";
    }

    @RequestMapping("/check")
    public String check(String username){
        System.out.println("username = " + username);
        User u = mapper.selectByUsername(username);
        return u==null?"用户名可用":"用户名已存在";
    }
}